package com.keqing.tomato;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.keqing.tomato.adapter.RadioListAdapter;
import com.keqing.tomato.bean.MySetting;
import com.keqing.tomato.bean.Sound;
import com.keqing.tomato.fragment.FragmentSettings;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class RadioListActivity extends AppCompatActivity {
    private String sound;
    private int time;
    private int[] data;
    private MediaPlayer mediaPlayer = new MediaPlayer();;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio_list);
        TextView mTitleTv = findViewById(R.id.title);
        SharedPreferences.Editor editor = getSharedPreferences("mysettings", MODE_PRIVATE).edit();
        TextView go_back = findViewById(R.id.go_back);
        go_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("sound", sound);
                intent.putExtra("time", time);
                setResult(10000, intent);
                RadioListActivity.this.finish();
            }
        });

        mTitleTv.setText(getIntent().getStringExtra("title"));

        int currentPos = -1;
        List<String> stringList = new ArrayList<>();
        int requestCode = getIntent().getIntExtra("requestCode", -1);
        if (requestCode == FragmentSettings.request_code_sound_focus || requestCode == FragmentSettings.request_code_sound_rest) {
            sound = getIntent().getStringExtra("sound");
            for (int i = 0; i < MySetting.soudList.size(); i++) {
                Sound soundBean = MySetting.soudList.get(i);
                stringList.add(soundBean.getCnName());
                if (soundBean.getCnName().equals(sound)) {
                    currentPos = i;
                }
            }
        }
        if (requestCode == FragmentSettings.request_code_time_focus || requestCode == FragmentSettings.request_code_time_rest) {
            time = getIntent().getIntExtra("time", -1);
            data = getIntent().getIntArrayExtra("data");
            for (int i = 0; i < data.length; i++) {
                stringList.add(data[i] + "分钟");
                if (data[i] == time) {
                    currentPos = i;
                }
            }
        }

        RadioListAdapter<String> adapter = new RadioListAdapter<>(stringList, currentPos, new RadioListAdapter.OnItemClickListener() {
            @Override
            public void onClick(int pos) {
                if (requestCode == FragmentSettings.request_code_sound_focus || requestCode == FragmentSettings.request_code_sound_rest) {
                    String filePath = MySetting.soudList.get(pos).getFilePath();
                    editor.putString("sound", filePath);
                    editor.apply();

                    sound = MySetting.soudList.get(pos).getCnName();
                    Intent intent = new Intent();
                    intent.putExtra("sound", sound);
                    setResult(0, intent);
                    try {
                        mediaPlayer.reset();
                        AssetFileDescriptor descriptor = getAssets().openFd(MySetting.soudList.get(pos).getFilePath());
                        mediaPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
                        mediaPlayer.prepare();
                        mediaPlayer.start();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    if (requestCode == FragmentSettings.request_code_time_focus){
                        time = data[pos];
                        editor.putInt("focusTime", time);
                        editor.apply();
                    }else{
                        time = data[pos];
                        editor.putInt("restTime", time);
                        editor.apply();
                    }
                    Intent intent = new Intent();
                    intent.putExtra("time", time);
                    setResult(0, intent);

                }
            }
        });

        RecyclerView recycler_view = findViewById(R.id.recycler_view);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        recycler_view.setLayoutManager(manager);
        recycler_view.setAdapter(adapter);
    }

    @Override
    public void finish() {
        super.finish();
        if (mediaPlayer != null) {
            mediaPlayer.stop();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mediaPlayer != null) {
            mediaPlayer.stop();
        }
    }
}